
const api = require("../../utils/app.js")
import requests from '../../utils/requests'

Page({
  data: {
    inputPassword: false,
    isLoading: false,
    account: '',
    password: '',
    name:''
  },
  onLoad: function () {
  },
  pwdFocus() {
    this.setData({
      inputPassword: true
    })
  },
  pwdBlur() {
    this.setData({
      inputPassword: false
    })
  },
  bindAccountInput(e) {
    let type = e.currentTarget.dataset.type; // data-key=value
    // console.log(event);
    this.setData({
      [type]: e.detail.value
    })
  },
  bindPasswordInput(e) {
    let type = e.currentTarget.dataset.type; // data-key=value
    // console.log(event);
    this.setData({
      [type]: e.detail.value
    })

  },
  async bindIdentity() {
    let {name, password} = this.data;
    
    this.setData({
      isLoading: true
    })
     // 后端验证
     let result = await requests('/login/login', {name})
     console.log(result)
     if(result.code === 200){ // 登录成功
       wx.showToast({
         title: '登录成功'
       })
       // 将用户的信息存储至本地
       wx.setStorageSync('userInfo', JSON.stringify(result.name))  
        // 跳转至个人中心personal页面
        wx.reLaunch({
          url: '/pages/personal/personal'
        })
      }else{
        wx.showToast({
          icon:'error',
          title: '账号或密码错误'
        })
      }

    setTimeout(() => {
      this.setData({
        isLoading: false
      })
    }, 1000)
  }
})